﻿using System;
using System.Drawing;
using System.Runtime.InteropServices.ComTypes;
using System.Text;

namespace Tsanie.ShellExtension {

    /// <summary>
    /// Thumbnail Handler 的基类
    /// </summary>
    public abstract class ThumbnailHandlerBase : IExtractImage, IExtractImage2, IPersistFile {
        private const int E_NOTIMPL = unchecked((int)0x80004001);

        #region - 私有字段 -

        private string m_FileName;
        private Size m_Size;
        private int m_ColorDepth;
        private ThumbnailFlags m_Flags;

        #endregion

        #region - 构造 -

        protected ThumbnailHandlerBase() { }

        #endregion

        #region - 公共属性 -

        /// <summary>
        /// 获取目标文件名
        /// </summary>
        public string Filename { get { return m_FileName; } }
        /// <summary>
        /// 获取或设置缩略标志
        /// </summary>
        public ThumbnailFlags Flags {
            get { return m_Flags; }
            set { m_Flags = value; }
        }
        /// <summary>
        /// 获取推荐的颜色深度
        /// </summary>
        public int RecomendedColorDepth { get { return m_ColorDepth; } }
        /// <summary>
        /// 获取推荐的缩略图大小
        /// </summary>
        public Size RecomendedSize { get { return m_Size; } }

        #endregion

        #region - 子类重写属性 -

        /// <summary>
        /// 返回缩略图
        /// </summary>
        protected abstract System.Drawing.Bitmap GetImage { get; }
        /// <summary>
        /// 图像最后修改时间
        /// </summary>
        protected virtual DateTime DateStamp { get { return DateTime.Now; } }
        protected virtual string GetThumbnailLocation { get { return this.Filename; } }
        /// <summary>
        /// 如果缩略图被异步生成, 这个表示缩略图生成线程的优先级
        /// </summary>
        protected virtual int GetPriority { get { return 0; } }
        protected virtual ThumbnailFlags GetFlag { get { return ThumbnailFlags.Cache | ThumbnailFlags.Async; } }

        #endregion

        #region - IPersistFile 成员 -

        void IPersistFile.GetClassID(out Guid pClassID) { pClassID = Guid.Empty; }
        void IPersistFile.GetCurFile(out string ppszFileName) { ppszFileName = null; }
        int IPersistFile.IsDirty() { return E_NOTIMPL; }
        void IPersistFile.Load(string pszFileName, int dwMode) { m_FileName = pszFileName; }
        void IPersistFile.Save(string pszFileName, bool fRemember) { }
        void IPersistFile.SaveCompleted(string pszFileName) { }

        #endregion

        #region - IExtractImage 成员 -

        public IntPtr Extract() { return this.GetImage.GetHbitmap(); }
        public void GetLocation(
            StringBuilder pszPathBuffer,
            int cch,
            out int pdwPriority,
            ref SIZE prgSize,
            int dwRecClrDepth,
            ref ThumbnailFlags pdwFlags
        ) {
            m_Size.Width = prgSize.cx;
            m_Size.Height = prgSize.cy;
            m_ColorDepth = dwRecClrDepth;
            // m_Flags = pdwFlags

            pdwFlags = this.GetFlag;
            pdwPriority = this.GetPriority;

            pszPathBuffer = new StringBuilder(this.GetThumbnailLocation);
        }
        long IExtractImage2.GetDateStamp() { return DateStamp.ToFileTime(); }

        #endregion

        #region - COM 组件注册 -

        [System.Runtime.InteropServices.ComRegisterFunction()]
        private static void ComReg(Type t) {
            Common.RegisterExtension(t, "{BB2E617C-0920-11d1-9A0B-00C04FC2D6C1}");
            
        }

        [System.Runtime.InteropServices.ComUnregisterFunction()]
        private static void ComUnreg(Type t) {
            Common.UnregisterExtension(t, "{BB2E617C-0920-11d1-9A0B-00C04FC2D6C1}");
        }

        #endregion

    }
}
